package class02;

import java.util.Stack;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class TwoStacksImplementQueue2 {

    public static class twoStackQueue{
        private Stack<Integer> stackpush;
        private Stack<Integer> stackpop;

        public twoStackQueue(){
            this.stackpush=new Stack<Integer>();
            this.stackpop=new Stack<Integer>();
        }

        public void PushToPop(){
            if (stackpop.isEmpty()){
                while (!stackpush.empty()){
                    stackpop.push(stackpush.pop());
                }
            }
        }

        public void add(int newNum){
            stackpush.push(newNum);
            PushToPop();
        }

        public int poll(){
            if (stackpop.empty()&&stackpush.empty()){
                throw new RuntimeException("Queue is empty!");
            }
            PushToPop();
            return stackpop.pop();
        }

        public int peek(){
            if (stackpop.empty()&&stackpush.empty()){
                throw new RuntimeException("Queue is empty!");
            }
            PushToPop();
            return stackpop.peek();
        }
    }
    public static void main(String[] args) {
        twoStackQueue test = new twoStackQueue();
        test.add(1);
        test.add(2);
        test.add(3);
        System.out.println(test.peek());
        System.out.println(test.poll());
        System.out.println(test.peek());
        System.out.println(test.poll());
        System.out.println(test.peek());
        System.out.println(test.poll());
    }
}
